﻿using Microsoft.AspNetCore.Mvc;
using MVC.Models;
using MVC.Models.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MVC.Controllers
{
    public class TestController : Controller
    {
        private readonly new_sContext _db;
        public TestController(new_sContext db)
        {
            _db = db;
        }

        [Route("/")]
        [Route("/index.html")]
        public IActionResult Index(string Name, string ClassName, int pages = 1)
        {
            ViewData["ClassName"] = "全部";
            if (ClassName == null || ClassName == "全部")
            {
                ViewData["cheng"] = _db.Information.Skip((pages - 1) * 4).Take(4).ToList();
                ViewData["row"] = (_db.Information.ToList().Count / 4).ToString();
            }
            else if (ClassName != null)
            {
                int ClassID = _db.Classes.Where(x => x.ClassName == ClassName).FirstOrDefault().ClassId;
                ViewData["cheng"] = _db.Information.Where(x=>x.ClassId==ClassID).Skip((pages - 1) * 4).Take(4).ToList();

                ViewData["row"] = (_db.Information.Where(x=>x.ClassId==ClassID).ToList().Count / 4).ToString();
            }
            if (Name != null)
            {
                ViewData["cheng"] = _db.Information.Where(x => x.Title.Contains(Name)||x.Content.Contains(Name)).Skip((pages - 1) * 4).Take(4).ToList();
                ViewData["row"] = (_db.Information.Where(x => x.Title.Contains(Name)||x.Content.Contains(Name)).ToList().Count / 4).ToString();
            }
            int row = 0;

            return View();
        }

        [Route("/detail.html")]
        public IActionResult detail(int id)
        {
            ViewData["news"] = _db.Information.FirstOrDefault(x => x.Id == id);
            return View();
        }

    }
}
